const app = getApp();
var utils = require('../../utils/util.js');
var MD5Util = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buyNum: 5, //限购件数
    product: {},
    times: {
      hour: 10,
      minute: 20,
      second: 20
    },
    chose: true, //点击兑换切换
    showmask: false, //点击出现弹窗
    jian: false,
    jia: true,
    num: 1, //需要购买产品个数
    exchange: true,//点击兑换
    point: 0,//消费总价
    canclick: true,
    font:'立即购买'

  },
  //点击出弹窗
  exchange() {
    if(this.data.product.state == 1){ //活动开始可以购买
      this.setData({
        showmask: true
      })
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'linear',
        delay: 0
      });
      animation.opacity(1).bottom(0).step();
      this.setData({
        ani: animation.export()
      })
    } else { //已结束或未开始
      this.setData({
        showmask: false
      })
    }
    
  },
  //关闭弹窗
  closeMask() {
    this.setData({
      showmask: false
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
      delay: 0
    });
    animation.opacity(0).bottom('-610rpx').step();
    this.setData({
      ani: animation.export()
    })
  },
  closeMaskRn() {
    this.closeMask()
    this.setData({
      chose: true
    })
  },
  //减
  reduce() {
    let i = this.data.num;
    i--;
    if (this.data.buyNum == 1 && i <= 1){
      i = 1;
      this.setData({
        jia: false,
        jian: false
      })
    } else if (this.data.buyNum != 1 && i <= 1){
      i = 1;
      this.setData({
        jian: false,
        jia: true,
      })
    } else if (i < this.data.buyNum) {
      this.setData({
        jia: true,
        jian: true,
      })
    }
    this.setData({
      num: i,
      point: i * this.data.price
    })
  },
  //加
  add() {
    let i = this.data.num;
    i++;
    if (i >= this.data.buyNum && this.data.buyNum == 1) { //限购一个
      i = this.data.product.buyNum;
      this.setData({
        jia: false,
        jian: false,
      })
    } else if (i >= this.data.buyNum && this.data.buyNum != 1){
      i = this.data.buyNum;
      this.setData({
        jia: false,
        jian: true,
      })
    } else if (i < this.data.buyNum){
      this.setData({
        jia: true,
        jian: true,
      })
    }
    this.setData({
      num: i,
      point: i * this.data.product.price
    })
    
  },
  //点击兑换
  exchangeFn() {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    var sucFunc = function (that, res) {
      if(res.data == 400){
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '网络错误，请稍后重试！',
          success: function (res) {
            return
          }
        })
      } else if (res.data == 402){
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '库存不足，看看其他商品吧！',
          success: function (res) {
            return
          }
        })
      }else{
        var suc = function (that, res) {
          console.log(res);
          wx.hideLoading();
          var str = String(res.data.timestamp)
          var sign = '';
          var signA = "appId=" + res.data.appid + "&nonceStr=" + res.data.noncestr + "&package=prepay_id=" + res.data.prepayid + "&signType=MD5&timeStamp=" + res.data.timestamp;
          var signB = signA + "&key=jzxtdqhbjzxtdqhbjzxtdqhbjzxtdqhb";
          sign = MD5Util.MD5(signB).toUpperCase();
          wx.requestPayment({
            'timeStamp': str,
            'nonceStr': res.data.noncestr,
            'package': "prepay_id=" + res.data.prepayid,
            'signType': 'MD5',
            'paySign': sign,
            'success': function (res) {
              that.setData({
                chose: false
              })
            },
            'fail': function (res) {
              console.log(res);
              wx.showModal({
                title: '提示',
                content: '支付失败，请稍后重试！',
                success: function (res) {
                  return
                }
              })
            }
          })
        }
        var res = { openid: app.d.openid, fee: that.data.num * that.data.product.s_price, order_on: res.data }
        utils.wxRequest(that, app.d.path, "Request/index", res, suc);

      }
      
    }
    var data = { 
      openid:app.d.openid ,
      goods_id: that.data.product.id,
      status:2,
      type:1,
      bay:1,
      num:that.data.num,
      price:that.data.num * that.data.product.s_price
    }
    utils.wxRequest(that, app.d.path, "Spike/order", data, sucFunc);
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    that.setData({
      imgPath: app.d.imgPath
    })
    var sucFunc = function (that, res) {
      that.setData({
        product: res.data.res,
        starttime: res.data.starttime,
        endtime:res.data.endtime
      })
      that.countdowns(res.data.starttime, res.data.endtime)
      wx.hideLoading();
      WxParse.wxParse('article', 'html', res.data.res.content, that, 5)
      if (that.data.buyNum <= 1) { //每人限购一个
        that.setData({
          jia: false,
          jian: false
        })
      } else {
        that.setData({
          jia: true,
          jian: false
        })
      }
      if (that.data.product.state == 0) {
        that.setData({
          font: '活动未开始'
        })
      } else if (that.data.product.state == 1) {
        that.setData({
          font: '立即购买'
        })
      } else if (that.data.product.state == 2) {
        that.setData({
          font: '活动已结束'
        })
      }
    }
    var data = { id: options.id }
    utils.wxRequest(that, app.d.path, "Spike/detail", data, sucFunc);
    
    
  },
  btn1: function () {
    wx.redirectTo({
      url: "/pages/coupon/coupon"
    })
  },
  countdowns: function (startTime, endtime) {
    var that = this;
    if (startTime > 0) {
      var time = startTime;
      that.setData({
        'product.state': 0
      })
    } else {
      var time = endtime
      that.setData({
        'product.state': 1
      })
    }
    
    that.setData({
      'times.hour': Math.floor(time / 3600 % 24),
      'times.minute': Math.floor(time / 60 % 60),
      'times.second': Math.floor(time % 60)
    })
    setTimeout(function () {
      that.setData({
        starttime: that.data.starttime - 1,
        endtime: that.data.endtime - 1
      });
      if (endtime < 0) {
        that.setData({
          'product.state': 2,
          'times.hour': '00',
          'times.minute': '00',
          'times.second': '00'
        })
        return false;
      }
      that.countdowns(that.data.starttime, that.data.endtime);
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})